<?php
namespace  App\Http\Controllers\Radius;
use  App\Http\Controllers\Controller;

use App\Model\Rad_line;
use App\Order;

use App\Teamqq;
use Illuminate\Http\Request;

use App\Common\ErrorDefine;

use App\Radacct;
use App\Radcheck;
use App\Radgroupcheck;
use App\Radreply;
use App\Radusergroup;
use App\Rad_manage;
use App\Radgroupreply;
use App\Rad_users;
use App\Rad_order;
use App\Line;
use DB;

use App\Agent;

use App\Http\Controllers\Radius\ModuleController;


class  RegionLineController extends Controller
{



    var $module;
    var $accessKeyId = 'LTAI77Yqw1AYSjcC';
    var $accessSecrec = 'fwnMvw1ZJWSWiP2chfulqwqa1F1THJ';


    public function __construct()
    {
        date_default_timezone_set("GMT");
    }




    /**
     *
     * 获取解析记录列表
     */
    public function DescribeDomainRecords()
    {
        $requestParams = array(
            "Action"    =>  "DescribeDomainRecords",
            "DomainName"    =>  "weji.vip"
        );
        return  $this->requestAli($requestParams);
    }



    /**
     * @return mixed
     * 获取子域名的解析记录列表
     */
    public function DescribeSubDomainRecords()
    {
        $requestParams = array(
            "Action"    =>  "DescribeSubDomainRecords",
            "SubDomain"    =>  "xxoo.weji.vip"
        );
        return  $this->requestAli($requestParams);
    }


    /**
     * @param $prefix
     * @return mixed
     * 添加解析记录
     */

    public function AddDomainRecord($prefix)
    {
        $requestParams = array(
            "Action"    =>  "AddDomainRecord",
            "DomainName"    =>  "weji.vip",
            "RR"    =>  $prefix,
            "Type"    =>  "CNAME",
            "Value"    =>  $prefix.".weji.vip",
        );



//        $requestParams = array(
//            "Action"    =>  "AddDomainRecord",
//            "DomainName"    =>  "weji.vip",
//            "RR"    =>  "zz",
//            "Type"    =>  "A",
//            "Value"    =>  "123.56.23.4",
//        );

        return  $this->requestAli($requestParams);
    }
    
    



    /**
     * @param $requestParams
     * @return mixed
     * 封装请求
     */
    private function requestAli($requestParams)
    {
        $publicParams = array(
            "Format"        =>  "JSON",
            "Version"       =>  "2015-01-09",
            "AccessKeyId"   =>  $this->accessKeyId,
            "Timestamp"     =>   date("Y-m-d\TH:i:s\Z"),
            "SignatureMethod"   =>  "HMAC-SHA1",
            "SignatureVersion"  =>  "1.0",
            "SignatureNonce"    =>  substr(md5(rand(1,99999999)),rand(1,9),14),
        );

        $params = array_merge($publicParams, $requestParams);
        $params['Signature'] =  $this->sign($params, $this->accessSecrec);
        $uri = http_build_query($params);
        $url = 'http://alidns.aliyuncs.com/?'.$uri;
        return $this->curl($url);
    }

    
    

    /**
     * @param $url
     * @return mixed
     * curl 发送
     */
    private function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $result=curl_exec ($ch);
        return $result;
    }


    /**
     * @param $params
     * @param $accessSecrec
     * @param string $method
     * @return string
     * 签名
     */
    private function sign($params, $accessSecrec, $method="GET")
    {
        ksort($params);
        $stringToSign = strtoupper($method).'&'.$this->percentEncode('/').'&';

        $tmp = "";
        foreach($params as $key=>$val){
            $tmp .= '&'.$this->percentEncode($key).'='.$this->percentEncode($val);
        }
        $tmp = trim($tmp, '&');
        $stringToSign = $stringToSign.$this->percentEncode($tmp);

        $key  = $accessSecrec.'&';
        $hmac = hash_hmac("sha1", $stringToSign, $key, true);

        return base64_encode($hmac);
    }


    /**
     * @param null $value
     * @return mixed|string
     * 字符编码
     */
    private function percentEncode($value=null)
    {
        $en = urlencode($value);
        $en = str_replace("+", "%20", $en);
        $en = str_replace("*", "%2A", $en);
        $en = str_replace("%7E", "~", $en);
        return $en;
    }



    private function outPut($msg)
    {
        echo date("Y-m-d H:i:s")."  ".$msg.PHP_EOL;
    }













}